<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Music/classes/PhpFox_ComponentAlbumDetailView.class.php, $Author:  $ */



/** Component for viewing featured songs
 *   Evgenij Grouk aka GloGen
 * @version $Id: PhpFox_ComponentAlbumDetailView.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.music
 */
class PhpFox_Mod_Music_ComponentAlbumDetailView extends BaseComponent
{
    function process()
    {
        $oAlbumItem    = &$this->_oModule->getItem('Album');
        /* @var $oMusicService PhpFox_Mod_Music_ItemAlbum */
        $oMusicService = &$this->_oModule->getService('Music');
        /* @var $oMusicService PhpFox_Mod_Music_ServiceMusic */
        $oTpl = &$this->_oModule->getTemplate();

        $oSongItm = &$this->_oModule->getItem('Song');

        $oReq = &App::getRequest();
        $iAlbumId = $oReq->getInt('id');

        $picPath = App::getParam('music.album.pic.path').$iAlbumId.'.jpg';
        $bAlbumPicExist = (file_exists($picPath))? true: false;

        $sAction = $oReq->getAction();
        if ($oReq->get('action')){
            $sAction = $oReq->get('action');
        }
        switch ($sAction){
            case 'album_save':{
               $iId = $oReq->getInt('album_id');
               if ( $iId == $iAlbumId)
               {
                    $aAlbumData = array();

                    $aSongs = $oReq->getArray('song_genre');

                    foreach ($aSongs as $iSongId => $iGenreId)
                    {
                        if ($oSongItm->load($iSongId))
                        {
                            $oSongItm->aData['genre_id'] = $iGenreId;
                            $oSongItm->update(array('genre_id'));
                        }
                    }

                    if ($oReq->get('musician_name')){
                        $aAlbumData['musician_name']     = $oReq->get('musician_name');
                    }
                    if ($oReq->get('album_title')){
                        $aAlbumData['album_title']       = $oReq->get('album_title');
                    }
                    if ($oReq->get('genre_sel')){
                        $aAlbumData['genre_id']          = $oReq->get('genre_sel');
                    }
                    if ($oReq->get('album_year')){
                        $aAlbumData['album_year']        = $oReq->get('album_year');
                    }
                    if ($oReq->get('album_description')){
                        $aAlbumData['album_description'] = $oReq->get('album_description');
                    }
                    if (!$oMusicService->saveAlbum($iAlbumId, $aAlbumData)){
                        $this->addErrors($oMusicService->getErrors());
                    }
                }
                }

                break;
            case 'album_deny':{
                    if (!$oMusicService->deleteAlbum($iAlbumId)){
                        $this->addErrors($oMusicService->getErrors());
                    }
                    App::gotoBackUrl('admin.music.albums');
                }

                break;
            case 'album_approve':{
                    if ($oMusicService->setAlbumApproved($iAlbumId)){
                        $this->addErrors($oMusicService->getErrors());
                    }
                    App::gotoBackUrl('admin.music.albums');
                }
                break;
            case 'delete_pic':{
                if ( $bAlbumPicExist ){
                    unlink($picPath);
                    $bAlbumPicExist = false;
                }
                }
                break;
            default :
                $sAction = '';
                break;
        }
        if ($sAction){
            App::getUrl();
        }


        list($aAlbum,$iCount) = $oMusicService->getAlbumsList(array('album_id'=>'='.$iAlbumId));

        $aSongs = $oMusicService->getSongNames(array('album_id'=>'='.$iAlbumId));


        $oTpl->assign(array(
            'aAlbum'         => $aAlbum[0],
            'sAlbumImageUrl' => App::getParam('music.album.pic.url'),
            'aSongs'         => $aSongs,
            'aGenres'        => $oMusicService->getGenres(),
            'sCurrentGenre'  => $aAlbum[0]['genre_id'],
            'bAlbumPic'      => $bAlbumPicExist,
        ));
        return $oTpl->fetch('AlbumDetail.html');
    }
}
?>